﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class WebControl_PageOn : System.Web.UI.UserControl
{

    public int total
    {
        set {
            if (this.labTotal.Text != value.ToString())
            {
                this.labTotal.Text = value.ToString();
                ShowPageInfo();
            }
            
        }
        get { return int.Parse(this.labTotal.Text); }
    }
    public int pageSize
    {
        set { this.hidePageSize.Value = value.ToString();  }
        get { return int.Parse(this.hidePageSize.Value); }
    }
    public int currentPage
    {
        set { this.labCurrent.Text = value.ToString(); }
        get { return int.Parse(this.labCurrent.Text); }
    }
    public int totalPage
    {
        set {   this.labTotalPage.Text = value.ToString();   }
        get { return int.Parse(this.labTotalPage.Text); }

    }
     
    private void ShowPageInfo()
    {
        int _totalPage, _total, _pageSize;
        _total = this.total;

        _pageSize = this.pageSize;
        _totalPage = _total / _pageSize;
        int remainder = _total % _pageSize;
        if (remainder > 0)
            _totalPage += 1;
        this.labTotal.Text = _total.ToString();

        this.labTotalPage.Text = _totalPage.ToString();

        if (this.currentPage <= 1)
        {
            this.currentPage = 1;
            this.btnLast.Enabled = false;
            this.btnFist.Enabled = false;
            if (_totalPage < 2)
            {
                this.btnEnd.Enabled = false;
                this.btnNext.Enabled = false;
            }
            else
            {
                this.btnEnd.Enabled = true;
                this.btnNext.Enabled = true;
            }
        }
        else if (this.currentPage >= _totalPage)
        {
            this.currentPage = _totalPage;
            this.btnEnd.Enabled = false;
            this.btnNext.Enabled = false;
            if (_totalPage > 1)
            {
                this.btnLast.Enabled = true;
                this.btnFist.Enabled = true;
            }
            else
            {
                this.btnLast.Enabled = false;
                this.btnFist.Enabled = false;
            }

        }
        else
        {
            this.btnLast.Enabled = true;
            this.btnFist.Enabled = true;
            this.btnEnd.Enabled = true;
            this.btnNext.Enabled = true;
        }
        
        
    }
    protected void Page_Load(object sender, EventArgs e)
    {                
        if (!Page.IsPostBack)
        {
            this.labCurrent.Text = "1";
            
        }
        
    }
    //定义一个委托事件
    public delegate void PageChangeEvent(object sender, EventArgs arg);
    //添加一个事件
    public event PageChangeEvent PageChanged;
    protected void OnPageChanged(Object sender, EventArgs e)
    {
        if (PageChanged != null)
            PageChanged(sender, e);
    }
    /// <summary>
    /// 首页
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnFist_Click(object sender, EventArgs e)
    {
        this.currentPage = 1;
        this.labCurrent.Text = "1";
        this.btnFist.Enabled = false;
        this.btnLast.Enabled = false;
        if (this.totalPage > 1)
        {
            this.btnNext.Enabled = true;
            this.btnEnd.Enabled = true;
        }
        PageChanged(sender, e);
    }
    protected void btnLast_Click(object sender, EventArgs e)
    {
        int _current = this.currentPage;
        _current -= 1;
        if (_current <= 1)
        {
            _current = 1;
            this.btnLast.Enabled = false;
            this.btnFist.Enabled = false;

        }
        if (this.totalPage > 1)
        {
            this.btnNext.Enabled = true;
            this.btnEnd.Enabled = true;
        }
        this.currentPage = _current;
        PageChanged(sender, e);
    }
    protected void btnNext_Click(object sender, EventArgs e)
    {
        int _current = this.currentPage;
        _current += 1;
        if (_current >= this.totalPage)
        {
            _current = this.totalPage;
            this.btnNext.Enabled = false;
            this.btnEnd.Enabled = false;
            
        }        
        this.btnFist.Enabled = true;
        this.btnLast.Enabled = true;
        this.currentPage = _current;
        PageChanged(sender, e);
    }
    /// <summary>
    /// 尾页
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnEnd_Click(object sender, EventArgs e)
    {
        this.currentPage = this.totalPage; 
        this.btnFist.Enabled = true;
        this.btnLast.Enabled = true;

        this.btnNext.Enabled = false;
        this.btnEnd.Enabled = false;

        PageChanged(sender, e);
    }
    protected void btnGo_Click(object sender, EventArgs e)
    {
        int pageGo = 1;
        if (Common.Help.IsNumber(this.txtPageGo.Text))
            pageGo = int.Parse(this.txtPageGo.Text);
        if (pageGo <= 0 )
        {
            pageGo = 1; 
        }
        if (pageGo > this.totalPage)
        {
            pageGo = this.totalPage;
        }
        this.currentPage = pageGo;
        if (pageGo == 1)
        {
            this.btnFist.Enabled = false;
            this.btnLast.Enabled = false;
            if(this.totalPage>1)            
            {
                this.btnEnd.Enabled = true;
                this.btnNext.Enabled = true;
            }
        }
        else if (pageGo == this.totalPage)
        {
            this.btnFist.Enabled = true;
            this.btnLast.Enabled = true;
            this.btnEnd.Enabled = false;
            this.btnNext.Enabled = false;

        }
        else
        {
            this.btnFist.Enabled = true;
            this.btnLast.Enabled = true;
            this.btnEnd.Enabled = true;
            this.btnNext.Enabled = true;
        }

        this.txtPageGo.Text = pageGo.ToString();
        PageChanged(sender, e);

    }
}
